/*Includes ----------------------------------------------*/
#include "b_config.h"
#include "ciu32l051_std.h"
#include "hal/inc/b_hal_pwm.h"
#include "b_os.h"
/**
 * \addtogroup B_MCU
 * \{
 */

/**
 * \addtogroup CIU32L051x
 * \{
 */

/**
 * \addtogroup PWM
 * \{
 */

/**
 * \defgroup PWM_Private_Variables
 * \{
 */



/**
 * \}
 */

/**
 * \addtogroup PWM_Private_Functions
 * \{
 */


/**
 * \}
 */

/**
 * \addtogroup PWM_Exported_Functions
 * \{
 */
#define TIM_PERIOD_VALUE (0xFFFF/50) //91HZ
#define TIM_PRESCALER_VALUE (0x07)
#define TIM_PULSE1_VALUE (uint32_t)(TIM_PERIOD_VALUE / 2)
int bMcuPwmInit(bPwmConfig_t *config)
{
    b_log("bMcuPwmInit %d\n", config->channel);
    if (config == NULL)
        return -1;
    if (config->channel == PWM_CHANNEL_1)
    {
        // 配置引脚
        std_gpio_init_t tim4_gpio_init = {0};

        std_rcc_gpio_clk_enable(RCC_PERIPH_CLK_GPIOA);
        tim4_gpio_init.pin       = GPIO_PIN_3;
        tim4_gpio_init.mode      = GPIO_MODE_ALTERNATE;
        tim4_gpio_init.pull      = GPIO_NOPULL;
        tim4_gpio_init.alternate = GPIO_AF2_TIM4;
        std_gpio_init(GPIOA, &tim4_gpio_init);
        // 配置tim4 pwm输出
        std_tim_basic_init_t          basic_init_struct = {0};
        std_tim_output_compare_init_t oc_config_struct  = {0};


        std_rcc_apb1_clk_enable(RCC_PERIPH_CLK_TIM4);

        basic_init_struct.prescaler           = TIM_PRESCALER_VALUE;
        basic_init_struct.counter_mode        = TIM_COUNTER_MODE_UP;
        basic_init_struct.period              = TIM_PERIOD_VALUE;
        basic_init_struct.clock_div           = TIM_CLOCK_DTS_DIV1;
        basic_init_struct.auto_reload_preload = TIM_AUTORELOAD_PRE_DISABLE;
        std_tim_init(TIM4, &basic_init_struct);

        oc_config_struct.output_compare_mode = TIM_OUTPUT_MODE_PWM1;
        oc_config_struct.output_pol          = TIM_OUTPUT_POL_HIGH;
        oc_config_struct.pulse               = TIM_PULSE1_VALUE;
        std_tim_output_compare_init(TIM4, &oc_config_struct, TIM_CHANNEL_2);
        b_log("pwm init ok------------------\n");
    }


    return 0;
}

int bMcuPwmDeInit(bPwmChannel_t channel)
{

    return 0;
}

int bMcuPwmStart(bPwmChannel_t channel)
{
    if (channel == PWM_CHANNEL_1)
    {
        std_tim_ccx_channel_enable(TIM4, TIM_CHANNEL_2);
        std_tim_enable(TIM4);
    }
    return 0;
}

int bMcuPwmStop(bPwmChannel_t channel)
{
    if (channel == PWM_CHANNEL_1)
    {
        std_tim_ccx_channel_disable(TIM4, TIM_CHANNEL_2);
        std_tim_disable(TIM4);
    }
    return 0;
}

int bMcuPwmSetDuty(bPwmChannel_t channel, uint8_t duty)
{
    if (channel == PWM_CHANNEL_1)
    {
        // 将duty(0-100)转换为实际的脉冲值
        uint32_t pulse = (uint32_t)((TIM_PERIOD_VALUE * duty) / 100);
        std_tim_set_ccx_value(TIM4, TIM_CHANNEL_2, pulse);
    }
    return 0;
}

int bMcuPwmSetFrequency(bPwmChannel_t channel, uint32_t frequency)
{
    if (channel == PWM_CHANNEL_1)
    {
        // 计算新的预分频值
        uint32_t prescaler = (SystemCoreClock / (TIM_PERIOD_VALUE * frequency)) - 1;
        if (prescaler > 0xFFFF)
        {
            return -1;  // 频率太低，无法实现
        }
        
        // 更新预分频值
       // std_tim_set_prescaler(TIM4, prescaler);
    }
    return 0;
}

/**
 * \}
 */

/**
 * \}
 */

/**
 * \}
 */

/**
 * \}
 */

/************************ Copyright (c) 2024 Bean *****END OF FILE****/